package com.wzm.system.controller;


import com.wzm.basic.util.AjaxResult;
import com.wzm.basic.util.PageList;
import com.wzm.system.domain.DictionaryDetail;
import com.wzm.system.query.DictionaryDetailQuery;
import com.wzm.system.service.DictionaryDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {

    @Autowired
    private DictionaryDetailService dictionaryDetailService;

    @GetMapping
    @ApiOperation(value = "查询所有字典类型")
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.findAll();
    }

   /* @GetMapping("/{id}")
    @ApiOperation(value = "查询单个字典")
    DictionaryDetail findOne(@PathVariable("id") Long id){
        return dictionaryDetailService.findOne(id);
    }*/

    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query){
        return dictionaryDetailService.queryPage(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除字典")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败了");
        }
    }

    @PutMapping
    @ApiOperation(value = "新增或修改字典")
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail.getId()==null){
                dictionaryDetailService.add(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败了");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败了");
        }
    }

    @GetMapping("/{id}")
    public List<DictionaryDetail> findAllById(@PathVariable("id") Long types_id){
        List<DictionaryDetail> details = dictionaryDetailService.findAllById(types_id);
        return details;
    }
}
